#include "WeatherSystem.h"
#include "ParticleManager.h"
#include "SGD Wrappers\CSGD_TextureManager.h"
#include "SGD Wrappers\CSGD_XAudio2.h"

WeatherSystem* WeatherSystem::pInstance = nullptr;

WeatherSystem::WeatherSystem(void)
{
	Key = "";
}


WeatherSystem::~WeatherSystem(void)
{
}

WeatherSystem* WeatherSystem::GetInstance(void)
{
	if(pInstance == nullptr)
		pInstance = new WeatherSystem();

	return pInstance;
}

void WeatherSystem::DeleteInstance()
{
	if(pInstance != nullptr)
	{
		delete pInstance;
		pInstance = nullptr;
	}
}

void WeatherSystem::ChangeTiles(string FileName)
{
	TileManager* grid = TileManager::GetInstance();

	if(FileName == "Rain.xml")
	{
		vector<string> Keys = ParticleManager::GetInstance()->GetKeys();
		vector<string>::iterator iter = Keys.begin();

		for( ; iter != Keys.end(); ++iter)
		{
			if(iter->find("Rain.xml") == 0)
			{
				break;
			}
			else
			{
				Keys.push_back("Rain.xml");
				ParticleManager::GetInstance()->SetKeys(Keys);
				ParticleManager::GetInstance()->Reset("Rain.xml");
				break;
			}
				
		}

		if(Keys.size() == 0)
		{
			Keys.push_back("Rain.xml");
			ParticleManager::GetInstance()->SetKeys(Keys);
			ParticleManager::GetInstance()->Reset("Rain.xml");
		}

		unsigned int gridX = grid->GetSizeX();
		unsigned int gridY = grid->GetSizeY();

		for(unsigned int i = 0; i < gridX; i++)
		{
			for(unsigned int j = 0; j < gridY; j++)
			{
				if(grid->GetTile(i, j)->GetTerrainType() == GRASS)
				{
					grid->GetTile(i, j)->ChangeTileType(WATER);
				}
				else if(grid->GetTile(i, j)->GetTerrainType() == DIRT)
				{
					grid->GetTile(i, j)->ChangeTileType(MUD);
				}
				else if(grid->GetTile(i, j)->GetTerrainType() == SNOW)
				{
					grid->GetTile(i, j)->ChangeTileType(WATER);
				}
			}
		}
	}
	else if(FileName == "Snow.xml")
	{
		vector<string> Keys = ParticleManager::GetInstance()->GetKeys();
		vector<string>::iterator iter = Keys.begin();

		for( ; iter != Keys.end(); ++iter)
		{
			if(iter->find("Snow.xml") == 0)
			{
				break;
			}
			else
			{
				Keys.push_back("Snow.xml");
				ParticleManager::GetInstance()->SetKeys(Keys);
				ParticleManager::GetInstance()->Reset("Snow.xml");
				break;
			}
				
		}

		if(Keys.size() == 0)
		{
			Keys.push_back("Snow.xml");
			ParticleManager::GetInstance()->SetKeys(Keys);
			ParticleManager::GetInstance()->Reset("Snow.xml");
		}

		unsigned int gridX = grid->GetSizeX();
		unsigned int gridY = grid->GetSizeY();

		for(unsigned int i = 0; i < gridX; i++)
		{
			for(unsigned int j = 0; j < gridY; j++)
			{
				if(grid->GetTile(i, j)->GetTerrainType() == GRASS)
				{
					grid->GetTile(i, j)->ChangeTileType(SNOW);
				}
				else if(grid->GetTile(i, j)->GetTerrainType() == WATER)
				{
					grid->GetTile(i, j)->ChangeTileType(SNOW);
				}
			}
		}
	}
	else if(FileName == "Sun.xml")
	{
		vector<string> Keys = ParticleManager::GetInstance()->GetKeys();
		vector<string>::iterator iter = Keys.begin();

		for( ; iter != Keys.end(); )
		{
			if(iter->find("Rain.xml") == 0)
			{
				iter = Keys.erase(iter);
				ParticleManager::GetInstance()->SetKeys(Keys);
			}
			else if(iter->find("Snow.xml") == 0)
			{
				iter = Keys.erase(iter);
				ParticleManager::GetInstance()->SetKeys(Keys);
			}
			else
				++iter;
		}


		unsigned int gridX = grid->GetSizeX();
		unsigned int gridY = grid->GetSizeY();

		for(unsigned int i = 0; i < gridX; i++)
		{
			for(unsigned int j = 0; j < gridY; j++)
			{
				if(grid->GetTile(i, j)->GetTerrainType() == WATER)
				{
					grid->GetTile(i, j)->ChangeTileType(GRASS);
				}
				else if(grid->GetTile(i, j)->GetTerrainType() == SNOW)
				{
					grid->GetTile(i, j)->ChangeTileType(GRASS);
				}
				else if(grid->GetTile(i, j)->GetTerrainType() == MUD)
				{
					grid->GetTile(i, j)->ChangeTileType(DIRT);
				}
			}
		}
	}
}

void WeatherSystem::ChangeWeather(string WeatherName)
{
	vector<string> Keys = ParticleManager::GetInstance()->GetKeys();
	vector<string>::iterator iter = Keys.begin();

	for( ; iter != Keys.end(); iter++)
	{
		if(iter->find(WeatherName) == 0)
		{
			ParticleManager::GetInstance()->Reset(WeatherName);
		}
	}

	SetKey(WeatherName);
	ChangeTiles(WeatherName);
}